package com.liu.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.config.RedisConfig;
import com.liu.dao.FoodsDao;
import com.liu.dto.FoodsDto;
import com.liu.entity.Foods;
import com.liu.service.intf.FoodsService;
import com.liu.utils.JedisUtils;
import com.liu.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class FoodsServiceImpl implements FoodsService {
    //实例化日志对象
    private Logger logger= LoggerFactory.getLogger(FoodsServiceImpl.class);
    @Resource
    private FoodsDao dao;
//    查询列表
    @Override
    public R querylist() {
        List<Foods> list;
//        1、查缓存(默认以综合条件查询
//        if (JedisUtils.esists(RedisConfig.FOODS_GOOD)){
//        list= JSON.parseArray(JedisUtils.getStr(RedisConfig.FOODS_GOOD),Foods.class);
//        }else {
//            缓存中没有，则查数据库
            list=dao.selectlist();
            JedisUtils.setStr(RedisConfig.FOODS_GOOD,JSON.toJSONString(list));
//        }
        return R.ok(list);
    }
//    按价格查询
    @Override
    public R queryByPrice() {
        List<Foods> list;
//        1、查缓存
//        if(JedisUtils.esists(RedisConfig.FOODS_PRICE)){
//            list=JSON.parseArray(JedisUtils.getStr(RedisConfig.FOODS_PRICE),Foods.class);
//        }else{
            list=dao.selectByPrice();
            JedisUtils.setStr(RedisConfig.FOODS_PRICE,JSON.toJSONString(list));
//        }
        return R.ok(list);
    }
//按销量查询
    @Override
    public R queryByBuy() {
        List<FoodsDto> list;
        list=dao.selectByBuy();
        return R.ok(list);
    }

    //    查看详情
    public R selectDetail(int id){
        if (id>0){
            FoodsDto foods=dao.detailById(id);
            return R.ok(foods);
        }
        else return R.fail("网络异常！");
    }
}
